// Copyright (C) 2009 Littlebear
// This file is part of the "Irrlicht Engine Plus".
// For conditions of distribution and use, see copyright notice in irrlicht.h

#include "CParticleSizeAffector.h"
#include "IAttributes.h"
#include "os.h"

namespace irr
{
namespace scene
{

CParticleSizeAffector::CParticleSizeAffector(f32 size)
	:Size(1.0f)
{
	#ifdef _DEBUG
	setDebugName("CParticleSizeAffector");
	#endif
}

//! Affects a particle.
void CParticleSizeAffector::affect(u32 now, SParticle* particlearray, u32 count)
{
	if (!Enabled)
		return;

	for (u32 i=0; i<count; ++i)
	{
		const u32 maxdiff = particlearray[i].endTime - particlearray[i].startTime;
		const u32 curdiff = now - particlearray[i].startTime;
		const f32 newscale = (f32)curdiff/maxdiff;
		particlearray[i].size = particlearray[i].startSize * Size;
	}
}

//! Writes attributes of the object.
//! Implement this to expose the attributes of your scene node animator for
//! scripting languages, editors, debuggers or xml serialization purposes.
void CParticleSizeAffector::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
{
	out->addFloat("Size", Size);
}

//! Reads attributes of the object.
//! Implement this to set the attributes of your scene node animator for
//! scripting languages, editors, debuggers or xml deserialization purposes.
//! \param startIndex: start index where to start reading attributes.
//! \return: returns last index of an attribute read by this affector
s32 CParticleSizeAffector::deserializeAttributes(s32 startIndex, io::IAttributes* in, io::SAttributeReadWriteOptions* options)
{
	const char* name = in->getAttributeName(startIndex);

	if (!name || strcmp(name, "Size"))
		return startIndex; // attribute not valid

	Size = in->getAttributeAsFloat(startIndex);
	++startIndex;

	return startIndex;
}



} // end namespace scene
} // end namespace irr
